/**
 * Created by Administrator on 2017/9/11.
 */

let publicDao = require('./publicDao.js');
let tableName = 'chip';

module.exports = chipDao;

function chipDao() {
    this.data = {};
    this.change = [];
}

chipDao.prototype.one = function (err, res, cb) {
    let _this = this;
    if (err !== null) {
        cb({code: err.code, msg: err.message}, null);
    } else if (res.length == 0) {
        cb(null, null);
    } else {
        _this.data = res[0];
        cb(null, {res: res[0]});
    }
};

chipDao.prototype.inId = function (id, cb) {
    //let publicModel = new publicDao();
    let _this = this;
    publicDao.init().table(tableName).wheres('id', '=', id).get(function (err, res) {
        _this.one(err, res, cb);
    });
};

chipDao.prototype.inChipId = function (userId, chipId, cb) {
    let _this = this;
    publicDao.init().table(tableName)
        .wheres('userId', '=', userId).wheres('chipId', '=', chipId).get(function (err, res) {
        _this.one(err, res, function (err1, res1) {
            if (!res1) {
                _this.set('userId', userId);
                _this.set('chipId', chipId);
                _this.set('num', 0);
                cb(err1, res1);
            } else {
                cb(err1, res1);
            }
        });
    });
};

chipDao.prototype.set = function (key, value) {
    this.data[key] = value;
    this.change.push(key);

    return this;
};

chipDao.prototype.addNum = function (num) {
    this.data.num += parseInt(num);
    this.change.push('num');

    return this;
};

chipDao.prototype.save = function (cb) {
    //console.error('data:' + JSON.stringify(this.data));
    //console.error('change:' + JSON.stringify(this.change));
    //let publicModel = new publicDao();
    if (!cb) {
        cb = function () {
        };
    }
    publicDao.init().table(tableName);
    if (!this.data.id) {
        for (let key in this.data) {
            publicDao.values(key, this.data[key]);
        }
        publicDao.insert(function (err, res) {
            if (err !== null) {
                cb({code: err.code, msg: err.message}, null);
            } else {
                cb(null, {res: res});
            }
        });
    } else {
        for (let i in this.change) {
            let key = this.change[i];
            //console.error(key + ':' + this.data[key]);
            publicDao.values(key, this.data[key]);
        }
        publicDao.wheres('id', '=', this.data.id);
        publicDao.update(function (err, res) {
            if (err !== null) {
                cb({code: err.code, msg: err.message}, null);
            } else {
                cb(null, {res: res});
            }
        });
    }
};

chipDao.prototype.delete = function (cb) {
    if (!this.data.id) {
        cb({code: 999, msg: 'not id'}, null);
    }
    publicDao.init().table(tableName).wheres('id', '=', this.data.id).delete(function (err, res) {
        if (err !== null) {
            cb({code: err.code, msg: err.message}, null);
        } else {
            cb(null, {res: res});
        }
    });
}